﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Ttifa.Infrastructure.Utils
{
    public class XmlHelper
    {
        public static string XmlSerialize<T>(T obj, string rootName = "")
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter tw = new StringWriter(sb))
            {
                Type type = obj.GetType();
                XmlSerializer xmlser = string.IsNullOrEmpty(rootName) ? new XmlSerializer(typeof(T)) :
                        new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
                xmlser.Serialize(tw, obj);
            }
            return sb.ToString();
        }

        public static T XmlDeserialize<T>(string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(typeof(T));
                return (T)xmldes.Deserialize(sr);
            }
        }
    }
}
